package com.zsinfo.guoss.supplier.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.FruitOfferBean;
import com.example.netcenter.bean.ResultsData;
import com.example.utilcenter.alertUtil.ToastUtil;
import com.example.utilcenter.event.MessageEvent;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by lixl on 2018/12/6.
 * <p>
 * desc:添加报价
 */
public class FruitOfferAddActivity extends BaseActivity {
    @BindView(R.id.account_container)
    RecyclerView account_container;
    @BindView(R.id.iv_shop_logo)
    ImageView iv_shop_logo;
    @BindView(R.id.fruit_name)
    TextView fruit_name;
    @BindView(R.id.tv_fruit_require)
    TextView tv_fruit_require;
    @BindView(R.id.tv_fruit_specification)
    TextView tv_fruit_specification;
    @BindView(R.id.et_fruit_offer)
    EditText et_fruit_offer;
    @BindView(R.id.et_fruit_mark)
    EditText et_fruit_mark;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_fruit_add;
    }

    @Override
    protected void initView() {
        setTitle("报价");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initNetData() {

        GetDatilInfo();

    }


    @OnClick({R.id.btn_add})
    void myClick() {


        String offerPrice = et_fruit_offer.getText().toString();

        if (offerPrice.isEmpty()) {

            ToastUtil.showSingleToast(this, "报价不能为空");

            return;
        }


        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().GOODS_QUOTED(ConstantsCode.goods_quoted, SharedPreferencesUtil.getID(),
                        getIntent().getStringExtra("goodId"), et_fruit_mark.getText().toString(), offerPrice),
                new Response() {
                    @Override
                    public void OK(Object data) {

                        EventBus.getDefault().post(new MessageEvent("刷新列表"));

                        ToastUtil.showSingleToast(FruitOfferAddActivity.this, "报价成功!");

                        finish();
                    }

                    @Override
                    public void Fail(Throwable e) {

                    }

                    @Override
                    public void complete() {

                    }
                });
    }

    private void GetDatilInfo() {


        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().GOOD_DETAILS(ConstantsCode.goods_details,
                        getIntent().getStringExtra("goodId")),
                new Response() {
                    @Override
                    public void OK(Object data) {
                        ResultsData<FruitOfferBean> product = (ResultsData<FruitOfferBean>) data;

                        Glide.with(FruitOfferAddActivity.this).load(product.getData().getLogo()).into(iv_shop_logo);

                        fruit_name.setText(product.getData().getName());

                        tv_fruit_require.setText(product.getData().getSourceCity());

                        tv_fruit_specification.setText(product.getData().getNorm());

                        if (product.getData().getProps().size() > 0) {

                            DynamicLayout(product.getData().getProps());

                        }

                    }

                    @Override
                    public void Fail(Throwable e) {

                    }

                    @Override
                    public void complete() {

                    }
                });

    }

    @Override
    protected void customDestroy() {


    }

    void DynamicLayout(List<FruitOfferBean.PropsBean> props) {

        account_container.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        CommonAdapter myAdapter = new CommonAdapter<FruitOfferBean.PropsBean>(getActivity(), R.layout.item_free_line, props) {
            @Override
            protected void convert(final ViewHolder holder, FruitOfferBean.PropsBean s, int position) {

                holder.setText(R.id.tv_key, s.getKey()).setText(R.id.tv_value, s.getValue());

            }
        };
        account_container.setAdapter(myAdapter);
    }

}



